//
//  XMChatImgCell.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/5.
//

#import "XMChatImgCell.h"

@interface XMChatImgCell ()

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) V2TIMMessage *message;

@end

@implementation XMChatImgCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEX(0xF7F7F7);
    [self addSubview:self.imgView];
}

- (void)setContentWithMessage:(V2TIMMessage *)message
                 senderAvatar:(NSString *)senderAvatarImg
               receiverAvatar:(NSString *)receiverAvatarImg {
    self.isSelf = message.isSelf;
    _message = message;
    if (message.elemType == V2TIM_ELEM_TYPE_IMAGE) {
        if (message.isSelf) {
            UIImage *img = [UIImage imageWithContentsOfFile:message.imageElem.path];
            CGSize imgSize;
            if (img.size.width > img.size.height) {
                if (img.size.width > KScaleWidth(224.0)) {
                    imgSize = CGSizeMake(KScaleWidth(224.0), img.size.height*KScaleWidth(224.0)/img.size.width);
                }
                else {
                    imgSize = img.size;
                }
            }
            else {
                if (img.size.height > KScaleWidth(224.0)) {
                    imgSize = CGSizeMake(img.size.width*KScaleWidth(224.0)/img.size.height, KScaleWidth(224.0));
                }
                else {
                    imgSize = img.size;
                }
            }
            self.avatarImg.x = kScreenWidth - KScaleWidth(10.0) - self.avatarImg.width;
            [self.avatarImg sd_setImageWithURL:[NSURL URLWithString:senderAvatarImg] placeholderImage:ImageNamed(@"mine_avatar_icon")];
            _imgView.frame = CGRectMake(self.avatarImg.x - KScaleWidth(10.0) - imgSize.width, _imgView.y, imgSize.width, imgSize.height);
            _imgView.image = img;
        }
        else {
            self.avatarImg.x = KScaleWidth(10.0);
            [self.avatarImg sd_setImageWithURL:[NSURL URLWithString:receiverAvatarImg] placeholderImage:ImageNamed(@"mine_avatar_icon")];
            V2TIMImage *imgModel = [message.imageElem.imageList lastObject];
            CGSize imgSize;
            if (imgModel.width > imgModel.height) {
                if (imgModel.width > KScaleWidth(224.0)) {
                    imgSize = CGSizeMake(KScaleWidth(224.0), imgModel.height*KScaleWidth(224.0)/imgModel.width);
                }
                else {
                    imgSize = CGSizeMake(imgModel.width, imgModel.height);
                }
            }
            else {
                if (imgModel.height > KScaleWidth(224.0)) {
                    imgSize = CGSizeMake(imgModel.width*KScaleWidth(224.0)/imgModel.height, KScaleWidth(224.0));
                }
                else {
                    imgSize = CGSizeMake(imgModel.width, imgModel.height);
                }
            }
            _imgView.frame = CGRectMake(self.avatarImg.maxX + KScaleWidth(10.0), _imgView.y, imgSize.width, imgSize.height);
            [_imgView sd_setImageWithURL:[NSURL URLWithString:imgModel.url]];
        }
    }
}

- (void)chatImgAction:(UITapGestureRecognizer *)tap {
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - KScaleWidth(10.0))/2, (kScreentHeight - KScaleWidth(10.0))/2, 10, 10)];
    img.backgroundColor = kColorHEX(0x000000);
    img.contentMode = UIViewContentModeScaleAspectFit;
    img.userInteractionEnabled = YES;
    if (_message.isSelf) {
        img.image = [UIImage imageWithContentsOfFile:_message.imageElem.path];
    }
    else {
        V2TIMImage *imgModel = [_message.imageElem.imageList firstObject];
        [img sd_setImageWithURL:[NSURL URLWithString:imgModel.url]];
    }
    UITapGestureRecognizer *imgDismiss = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgDismiss:)];
    [img addGestureRecognizer:imgDismiss];
    [[UIApplication sharedApplication].keyWindow addSubview:img];
    [UIView animateWithDuration:.3 animations:^{
        img.frame = CGRectMake(0, 0, kScreenWidth, kScreentHeight);
    }];
}

- (void)imgDismiss:(UITapGestureRecognizer *)tap {
    [UIView animateWithDuration:.3 animations:^{
        tap.view.frame = CGRectMake((kScreenWidth - KScaleWidth(10.0))/2, (kScreentHeight - KScaleWidth(10.0))/2, 10, 10);
    } completion:^(BOOL finished) {
        [tap.view removeFromSuperview];
    }];
}

- (UIImageView *)imgView {
    if (!_imgView) {
        _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.avatarImg.maxX + KScaleWidth(10.0), self.avatarImg.y, KScaleWidth(224.0), KScaleWidth(235.0))];
        _imgView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chatImgAction:)];
        [_imgView addGestureRecognizer:tap];
    }
    return _imgView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
